<?php
namespace Home\Controller;

use Think\Controller;

class CommonController extends Controller
{
    /*
    *自动执行的方法
    */
    public function _initialize()
    {
        $web_close = M('sys_config')->where('cfg_name="web_close"')->field('cfg_value')->find();
        if ($web_close['cfg_value']) {
            //组合访问的路径
            $path = CONTROLLER_NAME.'/'.ACTION_NAME;
            //判断是否需要进行权限验证
            if (in_array($path, C('AUTH_PATH'))) {
                //判断是否有登录会话
                if (!$_SESSION['uid']) {
                    //判断是否有cookie
                    if (!$_COOKIE['nickname']) {
                        //没登录也没COOKIE就跳转
                        redirect(U('home/login/index'));
                    } else {
                        $result = D('users')->where('uname="'.$_COOKIE['nickname'].'"')->relation(true)->find();
                        $data['login_time'] = time();
                        M('users')->where('uid="'.$result['uid'].'"')->save($data);
                        $_SESSION['email'] = $result['email'];
                        $_SESSION['nickname'] = $result['uname'];
                        $_SESSION['uid'] = $result['uid'];
                        $_SESSION['avatar'] = $result['uext']['avatar'];
                    }
                } else {
                    if (CONTROLLER_NAME == 'login' && ACTION_NAME == 'index') {
                        redirect(U('home/index/index'));
                    }
                }
            } else {
                if (!$_SESSION['uid'] && $_COOKIE['nickname']) {
                    $result = D('users')->where('uname="'.$_COOKIE['nickname'].'"')->relation(true)->find();
                    $data['login_time'] = time();
                    M('users')->where('uid="'.$result['uid'].'"')->save($data);
                    $_SESSION['email'] = $result['email'];
                    $_SESSION['nickname'] = $result['uname'];
                    $_SESSION['uid'] = $result['uid'];
                    $_SESSION['avatar'] = $result['uext']['avatar'];
                }
            }
        } else {
            layout(false);
            $this->assign('info', '本网站已关闭并正在调试中');
            $this->display('close');
            exit;
        }
    }
    //检测人类验证是否通过
    public function checkVercode()
    {
        if ($_POST['vercode'] == '' || $_POST['vercode'] != $_SESSION['answer']) {
            $arr = array('error'=>'人类验证不通过');
            echo json_encode($arr);
            exit;
        }
    }
    // 整理问题分类表
    public function cateTree($cateData, $fid = 0)
    {
        // return $cateData;
        $temp= array();
        foreach ($cateData as $value) {
            if ($value['fid'] == $fid) {
                $value['son'] = $this->cateTree($cateData, $value['cag_id']);
                $temp[] = $value;
            }
        }
        return $temp;
    }
    // 查询浏览量排行榜
    public function viewsRank($value = 5)
    {
        $viewssql = 'select t1.qid,t1.title,t2.views from question t1 left join question_ext t2 on t1.qid=t2.qid where t1.qtime>='.strtotime("-90 days").' order by t2.views desc limit '.$value;
        $views_rank = M()->query($viewssql);
        
        return $views_rank;
    }
    // 查询回复量排行榜
    public function ansnumRank($value = 5)
    {
        $ansnumsql = 'select t1.qid,t1.title,t2.num_of_ans from question t1 left join question_ext t2 on t1.qid=t2.qid where t1.qtime>='.strtotime("-90 days").' order by t2.num_of_ans desc,t1.uid limit '.$value;
        $ansnum_rank = M()->query($ansnumsql);
        
        return $ansnum_rank;
    }
    // 查询用户提问
    public function userQuestion($uid)
    {
        $sql = 'select t1.qid,t1.title,t1.qtime,t2.num_of_ans,t2.views from question t1 left join question_ext t2 on t1.qid=t2.qid where t1.qtime>='.strtotime("-3 days").' and t1.uid='.$uid.' order by t1.qtime,t1.qid desc';
        $res = M()->query($sql);
        
        return $res;
    }
    // 查询用户回答
    public function userAnswer($uid)
    {
        $sql = 'select t1.atime,t1.content,t2.qid,t2.title from answer t1 left join question t2 on t1.qid=t2.qid where t1.atime>='.strtotime("-3 days").' and t1.uid='.$uid.' order by t1.atime,t1.aid desc';
        $res = M()->query($sql);
        
        return $res;
    }
    // 查询用户回答总数
    public function userAnsnum($uid)
    {
        $time = strtotime('-30 days');
        $sql = 'select count(t1.uid) ucn,t1.uid,t2.uname,t3.avatar from answer t1 left join users t2 on t1.uid=t2.uid left join users_ext t3 on t1.uid=t3.uid where t1.atime>'.$time.' group by t1.uid,t3.avatar order by ucn desc,t1.uid limit 4';
        $res = M()->query($sql);
        
        return $res;
    }
    // 分页查询
    public function getPage($sql, $nowpage)
    {
        $list_rows = 5;
        $startpage = ($nowpage-1)*$list_rows;
        $result = M()->query($sql);
        $count = count($result);// 查询记录总数
        $sql = $sql.' limit '.$startpage.','.$list_rows;// 拼接分页语句
        $page_list = M()->query($sql);//获取分页
        $allpages = ceil($count / $list_rows);// 获取总页面数
        return ['allpages'=>$allpages,'nowpage'=>$nowpage,'page_list'=>$page_list];
    }
}
